package com.cincc.cinccimdemo.ui.main;

import android.content.Context;
import android.content.Intent;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.im.core.IMSipPhoneCtrl;

public class InviteFriendActivity extends IMBaseActivity {
    TextView introTextView;
    String userId = "";

    @Override
    protected int contentLayout() {
        return R.layout.contact_invite_activity;
    }


    @Override
    protected void bindViews() {
        super.bindViews();
        userId = getIntent().getStringExtra(AttrConstants.USER_ID);
        introTextView = findViewById(R.id.introTextView);
        findViewById(R.id.clearImageButton).setOnClickListener(v -> clear());
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        String remark = "我是 " + Constants.userInfo.nickName;
        introTextView.setText(remark);
    }

    @Override
    protected int menu() {
        return R.menu.contact_invite;
    }

    void clear() {
        introTextView.setText("");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.confirm) {
            invite();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void invite() {
        String remark = introTextView.getText().toString();
        IMSipPhoneCtrl.getInstance().applyAddFriend(userId, remark, "", imResponse -> {
            String result = imResponse.getCode() == 0 ? "申请添加好友成功" : imResponse.getMsg();
            runOnUiThread(() -> {
                Toast.makeText(InviteFriendActivity.this, result, Toast.LENGTH_SHORT).show();
                if (imResponse.getCode() == 0) {
                    finish();
                }
            });
        });
    }

    public static void inviteAddFriend(Context context, String userId) {
        Intent i = new Intent(context, InviteFriendActivity.class);
        i.putExtra(AttrConstants.USER_ID, userId);
        context.startActivity(i);
    }
}
